﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DO.DreamOnTrader.Trader;
using DO.DreamOnTrader;

namespace testTrader
{
	public partial class Form1 : Form
	{
		Trader trader = new Trader("C121114253", "730622");
		FutureOrder[] fos = { new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 6000f),
								new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 7000f),
							new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 7001f),
							new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 7002f),
							new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 7003f),
							new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 7004f),
							new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 7005f),
							new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 7006f),
							new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 7707f)};
		//FutureOrder fo = new FutureOrder(ActionSet.BUY, ProductSet.TX, "201203", 1, 6000f);

		public Form1()
		{
			InitializeComponent();

			DO.Common.Trace.SetLogFile("", "testTrader");

			try
			{
				trader.ConnectEvent += new TradeConnectEventHandler(printConnect);
				trader.DisconnectEvent += new TradeConnectEventHandler(printDisConnect);
			}
			catch (Exception ex)
			{
				DO.Common.Trace.ErrorWrite("testTrader", ex.Message);
			}
		}

		private void printOrders()
		{
			foreach (FutureOrder fo in fos)
			{
				DO.Common.Trace.ErrorWrite("testTrader", fo.ToString());
			}
		}

		private void printConnect(object sender, EventArgs e)
		{
			DO.Common.Trace.InfoWrite("testTrader", "登入成功");
		}

		private void printDisConnect(object sender, EventArgs e)
		{
			DO.Common.Trace.InfoWrite("testTrader", "離線");
		}

		private void button1_Click(object sender, EventArgs e)
		{
			
			try
			{
				trader.Login();
			}
			catch (Exception ex)
			{
				DO.Common.Trace.ErrorWrite("testTrader", ex.Message);
			}
		}

		private void button2_Click(object sender, EventArgs e)
		{
			
			try
			{
				trader.Logout();
			}
			catch (Exception ex)
			{
				DO.Common.Trace.ErrorWrite("testTrader", ex.Message);
			}
		}

		private void button3_Click(object sender, EventArgs e)
		{
			
			try
			{
				trader.sendFutureOrder(fos);
			}
			catch (Exception ex)
			{
				DO.Common.Trace.ErrorWrite("testTrader", ex.Message);
			}
		}

		private void button4_Click(object sender, EventArgs e)
		{
			printOrders();
		}

		private void button5_Click(object sender, EventArgs e)
		{
			foreach (FutureOrder fo in trader.historyFutureOrders)
			{
				DO.Common.Trace.ErrorWrite("trader->", fo.ToString());
			}
		}
	}
}
